﻿using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using Sirenix.Serialization;
using Sirenix.Utilities;
using Sirenix.Utilities.Editor;
using UnityEditor;
using UnityEngine;

namespace BTEditor
{
    [CreateAssetMenu(menuName = "My/Behaviour Tree")]
    public class BTAsset : ScriptableObject
    {
        private BehaviourTree serializeObj;
        
        [TableList(DrawScrollView = true, MaxScrollViewHeight = 400, MinScrollViewHeight = 100,AlwaysExpanded = true)]
        [Space(20)]
        public List<BlackboardData> DefineBlackboard = new List<BlackboardData>();

        [SerializeField]
        [HideInInspector]
        private byte[] serializeData;

        [Button("打开编辑器",ButtonSizes.Large,ButtonStyle.Box)]
        [PropertyOrder(-999)]
        private void OpenBTEditor()
        {
            BTMainWindows.OpenWindows(this);
        }
        
        public bool IsHaveData()
        {
            return serializeData != null;
        }

        public void Seriallized(BehaviourTree tree)
        {
            if (tree.IsAutoSave)
            {
                byte[] datas = SerializationUtility.SerializeValue(tree, DataFormat.Binary);
                serializeData = datas;
                this.name = tree.TreeFileName;
            }
        }

        public BehaviourTree DeSeriallized()
        {
            serializeObj = SerializationUtility.DeserializeValue<BehaviourTree>(serializeData, DataFormat.Binary);
            return serializeObj;
        }
    }
    
    [Serializable]
    public class BlackboardData : IValueDropdownItem
    {        
        [Space(20)]
        public string Key;
        
        [TextArea]
        public string Description;

        [VerticalGroup("Value")]
        [LabelText("值类型")]
        public ValueType ValueType;
        
        [VerticalGroup("Value")]
        [LabelText("Int值")]
        [ShowIf("ValueType",ValueType.Int)]
        public int Value_Int;
        [VerticalGroup("Value")]
        [LabelText("Float值")]
        [ShowIf("ValueType",ValueType.Float)]
        public float Value_Float;
        [VerticalGroup("Value")]
        [LabelText("Bool值")]
        [ShowIf("ValueType",ValueType.Bool)]
        public bool Value_Bool;

        public string GetText()
        {
            return $"{Key} : {Description}";
        }

        public object GetValue()
        {
            return this;
        }
    }

    public enum ValueType
    {
        Float,
        Int,
        Bool
    }
}

